﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace MyFound.Wpf.Windows
{
    /// <summary>
    /// AddFoundWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddFoundWindow : Window
    {
        public AddFoundWindow()
        {
            InitializeComponent();
        }

        private void TextBoxBase_OnTextChanged(object sender, TextChangedEventArgs e)
        {
            var code = (tbCode.Text ?? "").Trim();
            if (string.IsNullOrWhiteSpace(code) == false && Regex.IsMatch(code, @"\d{6}"))
            {
                var foundList = FoundApi.ListFound(new[] {code});
                if (foundList.ContainsKey(code))
                {
                    var found = foundList[code];
                    ShowFound(found);
                }
            }
            else
            {
                this.tbName.Clear();
            }
        }

        private void ShowFound(Class1 found)
        {
            this.tbName.Text = found.Name;
        }

        private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
        {
            var code = (tbCode.Text ?? "").Trim();
            if (string.IsNullOrWhiteSpace(code) == false && Regex.IsMatch(code, @"\d{6}"))
            {
                MyFound.Program.AddFound(code);
            }
            MessageBox.Show(@"添加成功", @"提示",MessageBoxButton.OK,MessageBoxImage.Information);
            this.tbName.Clear();
            this.tbCode.Clear();
        }

        private void AddFoundWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
             
        }
    }
}
